/*
 var STATUS_DELETED = 0;
 var STATUS_HAVE_NOT_SUBMIT = 1;
 var STATUS_HAVE_SUBMIT = 2;
 var STATUS_INFO_DISMISSED = 3;
 var STATUS_WAIT_REMITTANCE = 4;
 var STATUS_HAVE_REMITTANCE = 5;
 var STATUS_HALF_REAL_NAME = 8;
 var STATUS_REAL_NAME = 9;
 */

var OP_VIEW = "view";
var OP_MODIFY = "modify";
var OP_AUDIT = "audit";
var OP_DONEPAY = "paydone";
var OP_CHECKPAY = "pay";
var OP_ACCEPT = "accept";
var OP_REJECT = "reject";
var OP_SAVE = "save";
var OP_CLOSE = "close";
var OP_DELETE = "delete";
var OP_ADD = "add";
var OP_CHEHUI = "chehui";
var OP_HISTORY = "history";
var OP_EFFECT = "effect";
var OP_ADD_MEAL = "addmeal";
var OP_FREE_HISTORY = "freeHistory";
var OP_FREE_TRY = "freeTry";
//权限分配
var OP_RIGHT = "right";
//角色分配
var OP_ROLE_GIVE = "roleGive";
var OP_PASSWORD_RESET = "psReset";


function isUndefined(obj) {
    return ("undefined" == typeof(obj) || null == obj)
}


function stringIsEmpty(obj) {
    return ("undefined" == typeof(obj) || null == obj || "" == obj)
}


function checkResult(opts) {
    var obj = parseJson(opts);

    if (!parseResult(obj, opts)) {
        return null;
    }

    if (opts.success) {
        return opts.success(obj);
    }

    return obj;
}


function parseJson(opts) {
    if (stringIsEmpty(opts.json)) {
        opts.parseError();
        return null;
    }

    var obj = JSON.parse(opts.json);
    if (isUndefined(obj)) {
        opts.parseError();
        return null;
    }

    return obj;
}


function parseResult(result, opts) {
    //不存在errorcode的情况
    if (stringIsEmpty(result.errCode)) {
        return true;
    }

    if (0 != result.errCode) {
        opts.error(result.errCode, result.msg);
        return false;
    }

    return true;
}


function pathFetchName(path) {
    var pos = path.lastIndexOf("\\");
    if (-1 == pos) {
        pos = path.lastIndexOf("/");
    }

    if (-1 == pos) {
        return path;
    }

    return path.substring(pos + 1);
}


function guid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}


function log(info) {
}


function postRedirect(URL, PARAMS) {
    return __postRedirectTarget(URL, PARAMS, null);
}


function postRedirectWindow(URL, PARAMS) {
    return __postRedirectTarget(URL, PARAMS, "_blank");
}


function __postRedirectTarget(URL, PARAMS, target) {
    var temp = document.createElement("form");
    temp.action = URL;
    temp.method = "post";
    temp.style.display = "none";

    //设置target
    if (!isUndefined(target)) {
        temp.target = target;
    }

    for (var x in PARAMS) {
        var opt = document.createElement("textarea");
        opt.name = x;
        opt.value = PARAMS[x];
        temp.appendChild(opt);
    }
    document.body.appendChild(temp);
    temp.submit();
    return temp;
}


$.fn.maxLength = function (max) {
    this.each(function () {
        var type = this.tagName.toLowerCase();
        var inputType = this.type ? this.type.toLowerCase() : null;
        if (type == "input" && inputType == "text" || inputType == "password") {
            //Apply the standard maxLength
            this.maxLength = max;
        }
        else if (type == "textarea") {
            this.onkeypress = function (e) {
                var ob = e || event;
                var keyCode = ob.keyCode;
                var hasSelection = document.selection ? document.selection.createRange().text.length > 0 : this.selectionStart != this.selectionEnd;
                return !(this.value.length >= max && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection);
            };
            this.onkeyup = function () {
                if (this.value.length > max) {
                    this.value = this.value.substring(0, max);
                }
            };
        }
    });
};


String.prototype.subBytes = function (subLen) {
    if (subLen >= this.byteLength()) {
        return this;
    }

    var temp = this;
    var sub = 0;
    for (sub = 0; sub < this.length && subLen > 0; sub++) {
        if (temp.charCodeAt(sub) < 255) {
            subLen -= 1;
            continue;
        }

        if (subLen < 2) {
            break;
        }

        subLen -= 2;
    }

    return temp.slice(0, sub);
};


String.prototype.byteLength = function () {
    return this.replace(/[^\x00-\xff]/g, "**").length;
};

/**
 *
 * @param arrayBase
 *      基于该数组进行添加
 * @param arrayNew
 *      新加入的数组,新元素被置于数组头部
 * @param key
 *      进行比较的关键字
 * @returns {Array}
 */
function mergeArray(arrayBase, arrayNew, key) {

    var dst = $.extend(true, [], arrayBase);
    var newItems = [];

    //for (var i = 0; i < arrayNew.length; i++) {

    $(arrayNew).each(function (i) {
        //查找新加入的数组是否在原有数组中
        var similar = itemInArray(dst, arrayNew[i], key);

        //若不存在,则加入新的待加列表
        if (null == similar) {
            newItems.push(arrayNew[i]);
            return true;
            ;
        }

        //若存在,则和原有数据进行合并
        $.extend(similar, arrayNew[i]);
    })
    //}

    return newItems.concat(dst);
}

function getUrlParamValue(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)return unescape(r[2]);
    return null;
}

function resetDate(value,option){
    value = $.trim(value);
    var date = "" ;
    if(value){
        date = new Date(Date.parse(value.replace(/-/g, "/")));
        date.setDate(date.getDate() + option);
        return date.getFullYear() +"-"+ (date.getMonth()+1) +"-"+ date.getDate()
    }
   return value;

}
function itemInArray(array, item, key) {
    for (var i = 0; i < array.length; i++) {
        if (array[i][key] == item[key]) {
            return array[i];
        }
    }

    return null;
}


var Error = function () {
    function Impl(code, msg) {
        this.code = code;
        this.msg = msg;
    };

    Impl.prototype.clone = function () {
        return new Impl(this.code, this.msg);
    };

    Impl.error = function (code, msg) {
        return new Impl(code, msg);
    };

    Impl.SUCCESS = new Impl(0, "");
    Impl.CERT_EXIST = new Impl(-1, "证书不存在,请检查是否已插入Ukey");
    Impl.CTRL_LOAD = new Impl(-1, "无法正确加载控件");
    Impl.SIGN = new Impl(-1, "签名出现异常,验证失败");
    Impl.CERT_PASSWORD = new Impl(-1, "证书密码错误");
    Impl.KEYWORDS = new Impl(-1, "关键字不存在");

    Impl.network = function (url) {
        var msg = url ? "(" + url + ")" : "";
        return new Impl(-1, "网络异常" + msg);
    };
    Impl.format = function (stream) {
        var msg = stream ? "(" + stream + ")" : "";
        return new Impl(-1, "数据格式异常" + msg);
    }

    return Impl;
}();

